package com.snowRemovingUser.service.impl;

import com.snowRemovingUser.domain.TOperStatistics;
import com.snowRemovingUser.dto.CurveVo;
import com.snowRemovingUser.mapper.TOperStatisticsMapper;
import com.snowRemovingUser.service.ITOperStatisticsService;
import com.snowRemovingUser.util.OpeTypeToNameUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 操作统计Service业务层处理
 *
 * @author liguang
 * @date 2023-04-28
 */
@Service
public class TOperStatisticsServiceImpl implements ITOperStatisticsService
{


    @Autowired
    private TOperStatisticsMapper tOperStatisticsMapper;

    /**
     * 查询操作统计
     *
     * @param id 操作统计主键
     * @return 操作统计
     */
    @Override
    public TOperStatistics selectTOperStatisticsById(Long id)
    {
        return tOperStatisticsMapper.selectTOperStatisticsById(id);
    }

    /**
     * 查询操作统计列表
     *
     * @param tOperStatistics 操作统计
     * @return 操作统计
     */
    @Override
    public List<TOperStatistics> selectTOperStatisticsList(TOperStatistics tOperStatistics)
    {
        return tOperStatisticsMapper.selectTOperStatisticsList(tOperStatistics);
    }

    /**
     * 新增操作统计
     *
     * @param tOperStatistics 操作统计
     * @return 结果
     */
    @Override
    public int insertTOperStatistics(TOperStatistics tOperStatistics)
    {
        return tOperStatisticsMapper.insertTOperStatistics(tOperStatistics);
    }

    /**
     * 修改操作统计
     *
     * @param tOperStatistics 操作统计
     * @return 结果
     */
    @Override
    public int updateTOperStatistics(TOperStatistics tOperStatistics)
    {
        return tOperStatisticsMapper.updateTOperStatistics(tOperStatistics);
    }

    /**
     * 批量删除操作统计
     *
     * @param ids 需要删除的操作统计主键
     * @return 结果
     */
    @Override
    public int deleteTOperStatisticsByIds(Long[] ids)
    {
        return tOperStatisticsMapper.deleteTOperStatisticsByIds(ids);
    }

    /**
     * 删除操作统计信息
     *
     * @param id 操作统计主键
     * @return 结果
     */
    @Override
    public int deleteTOperStatisticsById(Long id)
    {
        return tOperStatisticsMapper.deleteTOperStatisticsById(id);
    }


    /**
     * v-charts 图型数据封装
     * @param tOperStatistics
     * @return
     */
    @Override
    public CurveVo selectTOperCurve(TOperStatistics tOperStatistics) {

        CurveVo curveVo = new CurveVo();
        List<String> columns = new ArrayList<>();
        columns.add("日期");

        List<Map> selected = new ArrayList<>();
        Map<String, Boolean> selectedMap = new HashMap<>();

        List<TOperStatistics> tOperStat = tOperStatisticsMapper.selectTOperStatisticsList(tOperStatistics);
        //将集合操作类型分类取key
        Map<Integer, List<TOperStatistics>> tOperStatByOperType = tOperStat.stream().collect(Collectors.groupingBy(TOperStatistics::getOperType));
        Set<Integer> OperTypes = tOperStatByOperType.keySet();
        //将所有的操作类型放入到columns中
        for (Integer operType : OperTypes) {

            String TypeName = OpeTypeToNameUtil.TypeToName(operType);
            columns.add(TypeName);

            //默认显示气象感知指标
            if (TypeName=="气象感知"){
                selectedMap.put(TypeName,true);
            }else {
                //其他默认不显示
                selectedMap.put(TypeName,false);
            }
        }
        //默认显示的指标
        selected.add(selectedMap);
        curveVo.setSelected(selected);

        //将集合按照时间分类
        Map<Date, List<TOperStatistics>> tOperStatByTime = tOperStat.stream().collect(Collectors.groupingBy(TOperStatistics::getOperTime));
        //按照时间排序
        Map<Date, List<TOperStatistics>> tOperStatByTimeSort = tOperStatByTime.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

        Set<Date> dates = tOperStatByTimeSort.keySet();
        List<Object> rowsList = new ArrayList<>();
        for (Date date : dates) {

            Map<String, Object> rows = new HashMap<>();

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String TimeStr = simpleDateFormat.format(date);
            rows.put("日期",TimeStr);
            //取出相同时间的数据
            List<TOperStatistics> tOpers = tOperStatByTime.get(date);

            for (TOperStatistics toper : tOpers) {

                String operTypeName = OpeTypeToNameUtil.TypeToName(toper.getOperType());
                rows.put(operTypeName,toper.getOperNo());
            }

            //将rows的key没有columns的元素 用0填充
            Set<String> keySet = rows.keySet();
            Set<String> diff = new HashSet<String>(columns);
            diff.removeAll(keySet);
            for (String dif : diff) {
                rows.put(dif,0);
            }

            rowsList.add(rows);
        }

        curveVo.setRows(rowsList);
        curveVo.setColumns(columns);
        return curveVo;
    }
}
